package com.bsoft.gol.hcs.clinical.dao;


import com.bsoft.gol.hcs.clinical.bean.response.GetDrugFrequencyResponse;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.reviewdrug.MedicationUseRate;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class MedicationUseRateDAO extends HibernateSupportDelegateDAO<MedicationUseRate> {
    public MedicationUseRateDAO() {
        this.setKeyField("rid");
    }

    @DAOMethod(sql = " from MedicationUseRate where rateId =:rateId ")
    public abstract List<MedicationUseRate> queryMedicationUseRate(@DAOParam("rateId") String rateId);

    @DAOMethod(sql = " from MedicationUseRate where orgId =:orgId and rateStatus=1 order by sort asc")
    public abstract List<MedicationUseRate> queryMedicationUseRateStatus(@DAOParam("orgId") String orgId);

    @DAOMethod(sql = " from MedicationUseRate where rateId =:rateId and orgId=:orgId")
    public abstract MedicationUseRate getMedicationUseRateByOrgId(@DAOParam("rateId") String rateId, @DAOParam("orgId") String orgId);

    @DAOMethod(sql = " from MedicationUseRate where dicItemId =:dicItemId and orgId=:orgId")
    public abstract MedicationUseRate getMedicationUseRateByDicItemId(@DAOParam("dicItemId") int dicItemId, @DAOParam("orgId") String orgId);

    public List<GetDrugFrequencyResponse> queryDrugFrequencyList(final String orgId) {
        HibernateStatelessResultAction<List<GetDrugFrequencyResponse>> action = new AbstractHibernateStatelessResultAction<List<GetDrugFrequencyResponse>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("SELECT\n" +
                        "\trate_cycle AS executionCycle,\n" +
                        "\trate_his_time AS executionTime,\n" +
                        "\trate_code AS frequencyCode,\n" +
                        "\trate_day AS frequencyCycle,\n" +
                        "\trate_times AS frequencyCycleNumber,\n" +
                        "\trate_remarks AS frequencyIllustrate,\n" +
                        "\trate_name AS frequencyName\n" +
                        "FROM\n" +
                        "\tmedication_use_rate\n" +
                        "WHERE\n" +
                        "\torg_id =:orgId\n" +
                        "AND rate_status = 1\n" +
                        "ORDER BY\n" +
                        "\tsort ASC ");
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("orgId", orgId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<GetDrugFrequencyResponse> drugFrequencyResponseList = new ArrayList<GetDrugFrequencyResponse>();
                List<Map> list = query.list();
                if (list.size() > 0) {
                    for (Map map : list) {
                        drugFrequencyResponseList.add(ConversionUtils.convert(map, GetDrugFrequencyResponse.class));
                    }
                }
                setResult(drugFrequencyResponseList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
